// pages/client/upload-contract/upload-contract.js
const app = getApp()
import { toast,throttle } from '../../../utils/Tools';
Page({
  data: {
    customer_id: null,
    agreement_code:null,
    agreement_id:'',
    customer_name:null,
    supply:null,
    require:null,
    fileList: []
  },
  onLoad: function (options) {
    this.setData({
      agreement_id: options.agreement_id||'',
      agreement_code: options.agreement_code||"",
      require: options.require||"",
      supply: options.supply||"",
      customer_id: options.customer_id||'',
      customer_name:options.customer_name||''
    })
    
    this.formSubmit = throttle(this.formSubmit,3000)
  },
  beforeRead(e) {
    const {
      file,
      callback
    } = e.detail;
    callback(file.type === 'image');
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          fileList: res.tempImagePath
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  chooseImage() {
    wx.chooseImage({
      count: 0,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        var tempFilePaths = res.tempFilePaths;
        for (let i = 0; i < tempFilePaths.length; i++) {
          this.data.fileList.push(tempFilePaths[i]);
        }
        this.setData({
          fileList: this.data.fileList
        })
      }
    })
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.fileList;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },

  // 删除图片
  deleteImg: function (e) {
    var pics = this.data.fileList;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    this.setData({
      fileList: pics,
    })
  },
  formSubmit(e) {
    const {contract_code,sign_address,supply,require} = e.detail.value
    if(this.data.fileList.length){
      for(let i=0;i<this.data.fileList.length;i++){
        let data = {}
        if(this.data.customer_id){
          data = {
            customer_id:this.data.customer_id,
            customer_name:this.data.customer_name,
            agreement_code:contract_code,
            agreement_address: sign_address,
            pay_company: supply,
            get_company: require,
            token: wx.getStorageSync('token')
          }
        }else{
          data = {
            agreement_id:this.data.agreement_id,
            agreement_code:contract_code,
            agreement_address: sign_address,
            pay_company: supply,
            get_company: require,
            token: wx.getStorageSync('token')
          }   
        }
        wx.uploadFile({
          url: app.globalData.BASE_URL+'api/orders/upload_agreement',
          filePath: this.data.fileList[i],
          name: `agreement_pic`,
          formData: data, 
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: res => {
            const data = JSON.parse(res.data)
            if(data.code === 200){
              toast('上传成功','success')
              setTimeout(()=>{
                wx.switchTab({
                  url: '/pages/client/client/client',
                })
              },1500)            
            }else{
              toast('上传失败','loading')
            }
          },
          fail: err =>{
            console.log(err)
          }
        })
      }
    }else{
      toast('提交失败,请先填写内容')
    }
  }
  //       表单验证        //
})